library(devtools)
library(usethis)
devtools::install_github("lbraglia/QScoring")
library(writexl)
library(lbscorer)
library(tidyverse)
library(haven)


sf12_df <- yasli3 %>%
  dplyr::select(
    q1_2,           # GH1 olacak
    q2_2,             # PF02
    q3_2,             # PF04
    q4_2,         # RP2
    q5_2,         # RP3
    q6_2,         # RE2
    q7_2,         # RE3
    q8_2,                  # BP2
    q9_2,                 # MH3
    q10_2,                # VT2
    q11_2,                # MH4
    q12_2                 # SF2
  )


names(sf12_df) <- c(
  "GH1","PF02","PF04","RP2","RP3",
  "RE2","RE3","BP2","MH3","VT2","MH4","SF2"
)
head(sf12_df)


# sf12_df adlı veri çerçevenizde 12 madde var:
sf12_skorlar <- sf12(sf12_df)

# Sonuçları inceleyin
head(sf12_skorlar)
view(sf12_skorlar)

yasli3_sf12 <- cbind(yasli3, sf12_skorlar)
write_xlsx(yasli3_sf12, "yasli3_sf12.xlsx")

yasli4_sf12 <- yasli3_sf12 %>% 
  dplyr::select(
    country,
    ID,
    sex_t1,
    age_t2,
    marital_status_t3,
    born_t4,
    education_t5,
    income_t6,
    life_style_t7,
    chronic_illness_t8,
    experienced_t9,
    satisfy_living_environment_10,
    t11_2,
    t12_2,
    t13_2,
    t14_2,
    t15_2,
    t16_2,
    t17_2,
    t18_2,
    t19_2,
    t20_2,
    t21_2,
    t22_2,
    t23_2,
    t24_2,
    t25_2,
    Phys_Fraility,
    Psyc_Fraility,
    Social_Fraility,
    Total_Fraility,
    l1_2,
    l2_2t,
    l3_2t,
    l4_2,
    l5_2t,
    l6_2t,
    l7_2,
    l8_2,
    l9_2t,
    l10_2t,
    l11_2,
    q1_2,           # GH1 olacak
    q2_2,             # PF02
    q3_2,             # PF04
    q4_2,         # RP2
    q5_2,         # RP3
    q6_2,         # RE2
    q7_2,         # RE3
    q8_2,                  # BP2
    q9_2,                 # MH3
    q10_2,                # VT2
    q11_2,                # MH4
    q12_2,
    PCS12,
    MCS12
  )

glimpse(yasli4_sf12)



yasli <- yasli4_sf12 %>% 
  dplyr::mutate(
    social_loneliness = l1_2 + l4_2 + l7_2 + l8_2 + l11_2,
    emotional_loneliness = l2_2t + l3_2t + l5_2t + l6_2t + l9_2t + l10_2t,
    total_lonliness = social_loneliness + emotional_loneliness,
    Total_QoL = PCS12 + MCS12
  )



yasli <- yasli %>% 
  relocate(social_loneliness, emotional_loneliness, total_lonliness, .before = 43)

write_sav(yasli, "yasli.sav")

colnames(yasli)


##### INCOME 

                          # fazla boşlukları tekleştir
yasli$income_t6 <- yasli$income_t6 %>%
  iconv(from = "UTF-8", to = "ASCII//TRANSLIT") %>%   # UTF karakterleri sadeleştir
  gsub("[[:space:]]", " ", .) %>%                     # tüm boşluk türlerini normalleştir
  gsub("[–—−-]", "-", .) %>%                          # her türlü tireyi "-" yap
  gsub(" +", " ", .) %>%                              # fazla boşlukları tekleştir
  trimws()                                            # baş/son boşlukları sil


unique(yasli$income_t6)


library(dplyr)

# Türkiye ve Almanya gelir seviyeleri
tl_levels <- c(
  "5.000,-TL veya daha az",
  "5.001,-TL - 10.000,-TL",
  "10.001,-TL - 15.000,-TL",
  "15.001,-TL - 20.000,-TL",
  "20.001,-TL - 25.000,-TL",
  "25.001,-TL - 30.000,-TL",
  "30.001,-TL - 35.000,-TL",
  "35.000,-TL - ve fazlasi"
)

eur_levels <- c(
  "200,-EUR veya daha az",
  "251,-EUR - 500,-EUR",
  "501,-EUR - 1.000,-EUR",
  "1.001,-EUR - 1.500,-EUR",
  "1.501,-EUR - 2.000,-EUR",
  "2.001,-EUR - 3.000,-EUR",
  "3.000,-EUR - ve fazlasi"
)



yasli <- yasli %>%
  mutate(
    income_t6_tr = factor(income_t6, 
                          levels = tl_levels, 
                          ordered = TRUE),
    income_t6_de = factor(income_t6, 
                          levels = eur_levels, 
                          ordered = TRUE),
    income_num = case_when(
      country == 1 ~ as.numeric(income_t6_tr),
      country == 2 ~ as.numeric(income_t6_de)
    )
  ) %>%
  group_by(country) %>%
  mutate(income_z = as.numeric(scale(income_num))) %>%
  ungroup()



yasli %>%
  group_by(country) %>%
  summarise(
    mean_income = mean(income_z, na.rm = TRUE),
    sd_income = sd(income_z, na.rm = TRUE),
    n_missing = sum(is.na(income_z))
  )



## Reverse code
yasli <- yasli %>%
  mutate(
    # 1–5 aralığı
    q1_2_rev = 6 - q1_2,
    q8_2_rev = 6 - q8_2,
    
    # 1–6 aralığı
    q9_2_rev  = 7 - q9_2,
    q10_2_rev = 7 - q10_2
  )

colnames(yasli)
## Make numeric

library(haven)
library(labelled)

yasli <- yasli %>%
  mutate(
    # country: 1=Türkiye, 2=Almanya → 0/1
    country_num = ifelse(country %in% c(1, "1", "Türkiye"), 0, 1),
    country_num = labelled(country_num,
                           labels = c("Türkiye" = 0, "Almanya" = 1)),
    
    # sex: Erkek=0, Kadın=1
    sex_num = ifelse(sex_t1 == "Kadın", 1, 0),
    sex_num = labelled(sex_num,
                       labels = c("Erkek" = 0, "Kadın" = 1)),
    
    # marital status: 4 kategori (ikili DEĞİL)
    marital_num = case_when(
      marital_status_t3 == "Evli / birlikte yaşıyorum" ~ 0L,
      marital_status_t3 == "Bekar"                      ~ 1L,
      grepl("Ayrılmış|boşanmış", marital_status_t3)     ~ 2L,
      marital_status_t3 == "Dul"                        ~ 3L,
      TRUE ~ NA_integer_
    ),
    marital_num = labelled(marital_num,
                           labels = c("Evli/birlikte" = 0,
                                      "Bekar"        = 1,
                                      "Ayrılmış/boşanmış" = 2,
                                      "Dul"          = 3)),
    
    # education: ordinal 0–3
    edu_num = case_when(
      education_t5 == "Hiç veya ilkokul"           ~ 0L,
      education_t5 == "Ortaokul/lise"              ~ 1L,
      education_t5 == "Üniversite (Lisans)"        ~ 2L,
      education_t5 == "Lisansüstü"                 ~ 3L,
      TRUE ~ NA_integer_
    ),
    edu_num = labelled(edu_num,
                       labels = c("İlk/İlkokul" = 0,
                                  "Ortaokul/Lise" = 1,
                                  "Üniversite (Lisans)" = 2,
                                  "Lisansüstü" = 3))
  )

yasli <- yasli %>%
  mutate(
    country_n = as.numeric(country_num),
    sex_n     = as.numeric(sex_num),
    marital_n = as.numeric(marital_num),
    edu_n     = as.numeric(edu_num),
    income_n  = as.numeric(income_z)
  )


val_labels(yasli$country_num)
val_labels(yasli$sex_num)
val_labels(yasli$marital_num)
val_labels(yasli$edu_num)





colnames(yasli)

library(dplyr)

yasli %>%
  group_by(country_n) %>%
  summarise(
    n_total = n(),
    n_female = sum(sex_n == 0, na.rm = TRUE),
    n_male   = sum(sex_n == 1, na.rm = TRUE),
    mean_age = mean(age_t2, na.rm = TRUE),
    sd_age   = sd(age_t2, na.rm = TRUE),
    marital_mean = mean(marital_n, na.rm = TRUE),
    edu_mean     = mean(income_z, na.rm = TRUE),
    income_mean  = mean(income_n, na.rm = TRUE)
  )

library(dplyr)

yasli %>%
  group_by(country_n, education_t5) %>%
  summarise(n = n()) %>%
  mutate(percentage = round(100 * n / sum(n), 1)) %>%
  arrange(country_n, education_t5)

unique(yasli$education_t5)
